from io import StringIO
from io import BytesIO
# try:
#     f=open('soap1.txt','r',encoding='utf-8',errors='ignore')
#     print(f.read())
# finally:
#     if f:
#         f.close()

#with

#读文件
def read_f():
    with open('soap.txt','r',encoding='utf-8',errors='ignore') as f:
        print(f.read())
        print(f.read(1024))
        for line in f.readline():#一行一行读取
            print(line.strip())
        for line in f.readlines():#整个读取 保存为list
            print(line.strip())
# open('soap.txt','rb',encoding='utf-8',errors='ignore')#二进制文件

#写文件
def wirte_f():
    with open('soap.txt','w',encoding='utf-8',errors='ignore') as f:
        f.write('hello world')

# 很多时候，数据读写不一定是文件，也可以在内存中读写

#StringIO 与 BytesIO
fs=StringIO()
fs.write('hello world')

print(fs.getvalue())

bf = BytesIO()
bf.write('中文'.encode('utf-8'))
print(bf.getvalue())
